// MegaDrawer.cpp : implementation file
//

#include "stdafx.h"
#include "resource.h"
#include "MegaDrawer.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMegaDrawer

CMegaDrawer::CMegaDrawer()
{
	m_pBackgroundBmp = m_pSkinSingle = m_pSkinDouble = NULL;
	m_nSize = 1;
	m_bActive = false;
	m_bOnTop = false;

	m_pSkinSingle=m_pSkinDouble=m_pSkinSingleUn=m_pSkinDoubleUn=0;
	m_StreamboxPower.LoadBitmap(IDB_STREAMBOX_POWER);
}

CMegaDrawer::~CMegaDrawer()
{
	if(m_pSkinSingle) delete m_pSkinSingle;
	if(m_pSkinDouble) delete m_pSkinDouble;
	if(m_pSkinSingleUn) delete m_pSkinSingleUn;
	if(m_pSkinDoubleUn) delete m_pSkinDoubleUn;
}

void CMegaDrawer::SetBitmaps(
	UINT uiSkinSingle, UINT uiSkinDouble, UINT uiSkinSingleUn, UINT uiSkinDoubleUn)
{
	m_pSkinSingle = new CMegaBitmap();
	m_pSkinDouble = new CMegaBitmap();
	m_pSkinSingleUn = new CMegaBitmap();
	m_pSkinDoubleUn = new CMegaBitmap();
	m_pSkinSingle->LoadBitmap(uiSkinSingle);
	m_pSkinDouble->LoadBitmap(uiSkinDouble);
	m_pSkinSingleUn->LoadBitmap(uiSkinSingleUn);
	m_pSkinDoubleUn->LoadBitmap(uiSkinDoubleUn);

	m_hrgnSingle = m_pSkinSingle->CreateRegion(RGB(255, 0, 0));
	m_hrgnDouble = m_pSkinDouble->CreateRegion(RGB(255, 0, 0));
	m_hrgnSingleUn = m_pSkinSingleUn->CreateRegion(RGB(255, 0, 0));
	m_hrgnDoubleUn = m_pSkinDoubleUn->CreateRegion(RGB(255, 0, 0));

	RecalcRegion();
}

void CMegaDrawer::RecalcRegion()
{
	switch(m_nSize)
	{
	case 1: m_pBackgroundBmp = (m_bActive?m_pSkinSingle:m_pSkinSingleUn); break;
	case 2: m_pBackgroundBmp = (m_bActive?m_pSkinDouble:m_pSkinDoubleUn); break;
	}

	CSize sz = m_pBackgroundBmp->GetBitmapDimension();
	SetWindowPos(NULL, 0, 0, sz.cx, sz.cy, SWP_NOZORDER|SWP_NOMOVE);

	HRGN hrgn = ::CreateRectRgn(0, 0, 0, 0);
	switch(m_nSize)
	{
	case 1: 
		::CombineRgn(
			hrgn, 
			(m_bActive?m_hrgnSingle:m_hrgnSingleUn), 
			(m_bActive?m_hrgnSingle:m_hrgnSingleUn), 
			RGN_COPY);
		SetWindowRgn(hrgn, FALSE); 
		break;
	case 2: 
		::CombineRgn(
			hrgn, 
			(m_bActive?m_hrgnDouble:m_hrgnDoubleUn), 
			(m_bActive?m_hrgnDouble:m_hrgnDoubleUn), 
			RGN_COPY);
		SetWindowRgn(hrgn, FALSE); 
		break;
	}
}

void CMegaDrawer::SetSize(int nSize)
{
	m_nSize = nSize;
	RecalcRegion();

	RepositionControls();
}

void CMegaDrawer::Activate(bool bActive)
{
	m_bActive = bActive;
	RecalcRegion();

	RepositionControls();
}

bool CMegaDrawer::IsActive()
{
	return m_bActive;
}

void CMegaDrawer::MoveToTop(bool bOnTop)
{
	if(bOnTop)
		BringWindowToTop();
	m_bOnTop = bOnTop;
}

bool CMegaDrawer::IsOnTop()
{
	return m_bOnTop;
}


CMegaBitmap * CMegaDrawer::GetCurrentSkin()
{
	return m_pBackgroundBmp;
}

BEGIN_MESSAGE_MAP(CMegaDrawer, CWnd)
	//{{AFX_MSG_MAP(CMegaDrawer)
	ON_WM_ERASEBKGND()
	ON_WM_LBUTTONDOWN()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CMegaDrawer message handlers

BOOL CMegaDrawer::OnEraseBkgnd(CDC* pDC) 
{
	// TODO: Add your message handler code here and/or call default
	m_pBackgroundBmp->DrawTransparent(pDC, 0, 0, RGB(255, 0, 0));

	// in OEM versions - draw "powered by Streambox"
#ifdef _OEM_
	if(IsOnTop())
	{
		CRect rc;
		GetClientRect(&rc);
		CSize sz = m_StreamboxPower.GetBitmapDimension();
		m_StreamboxPower.Draw(pDC, rc.right-sz.cx-12, rc.bottom-45-sz.cy+12);
	}
#endif

	return 1;
}

void CMegaDrawer::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	// change active status
	CSize sz = m_pBackgroundBmp->GetBitmapDimension();
	if(point.y >= sz.cy - 45)
	{
#ifdef _OEM_
		CRect rc;
		GetClientRect(&rc);
		CSize szPower = m_StreamboxPower.GetBitmapDimension();
		if(
			(point.x >= (rc.right-szPower.cx-12)) && (point.x <= (rc.right-12))&&
			(point.y >= (rc.bottom-szPower.cy-32)) && (point.y <= (rc.bottom-32))
			)
		{
			::ShellExecute(GetSafeHwnd(), "open", "http:\\\\www.streambox.com", NULL, NULL, SW_SHOWDEFAULT);
			return;
		}

#endif

		GetParent()->SendMessage(WM_DRAWER_ACTIVATION, (WPARAM) this);
	}
}

void CMegaDrawer::OnMouseMove(UINT nFlags, CPoint point) 
{
#ifdef _OEM_
	CSize sz = m_pBackgroundBmp->GetBitmapDimension();
	if(point.y >= sz.cy - 45)
	{
		CRect rc;
		GetClientRect(&rc);
		CSize szPower = m_StreamboxPower.GetBitmapDimension();
		if(point.x >= rc.right-szPower.cx-12 && point.x <= rc.right-12)
			::SetCursor(::LoadCursor(/*::AfxGetApp()->m_hInstance*/::AfxGetInstanceHandle(), MAKEINTRESOURCE(IDC_HANDARROW)));
	}
#endif
}
